環境準備好,就可以抓Yahoo Finance上面的資料囉!為什麼不用Google呢?因為Google已經沒有提供API了,所以轉而向Yahoo要。
本篇會教如何取得網路資料,會以Yahoo Finance為範例。會參考前輩的網站,這裡面很多寶物,大家可以去挖喔!
先複習一下,我們Day4安裝完後,應該會在Pipfile
內顯示:
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages]
requests = "*"
numpy = "*"
pandas = "*"
[requires]
python_version = "3.8"
其中packages
內顯示的就是我們專案所需的套件,確認我們都有安裝好,就去Pipfile.lock
查看這三個是否有記錄,有的話表示安裝好。沒有的話使用pipenv install
,他會根據packages
去檢查Pipfile.lock
是否都有,如果沒有或是版本不對就安裝。
接著我們從Yahoo Finance那邊取得資料,需要呼叫他們的API:https://query1.finance.yahoo.com/v8/finance/chart/2330.TW?period1=0&period2=1549258857&interval=1d&events=history&=hP2rOschxO0
首先,要設定好我們所要送出的請求。其中要設定的參數是股票代碼和日期。
股票代碼是2317.TW
,2317
是鴻海的代碼,而TW
是臺灣證券交易所,從Day3可以明顯看得出來代碼方式與Google不同,這個組合僅適合用在Yahoo Finance。period1
起始日,數值為1970年後的秒數,而period2
是結束日,也是1970年後的秒數。而這次範例,則是從2020/9/1
算到2020/9/13
,也就是1598889600~1599926400秒,因此period1=1598889600
和period2=1599926400
:
# API位置
address = "https://query1.finance.yahoo.com/v8/finance/chart/2317.TW?period1=0&period2=1599926400&interval=1d&events=history&=hP2rOschxO0"
設定好我們要取資料的來源後,接著就是用requests
的get
去跟Yahoo取資料:
# 使用requests 來跟遠端 API server 索取資料
response = requests.get(address)
最後再印出來,就會得到鴻海從9/1到9/13的股價:
print(response.text)
Yahoo Finance會回傳這樣的json檔,我們的歷史資料就取得囉~
{
"chart": {
"result": [
{
"meta": {
"currency": "TWD",
"symbol": "2317.TW",
"exchangeName": "TAI",
"instrumentType": "EQUITY",
"firstTradeDate": 726195600,
"regularMarketTime": 1599802203,
"gmtoffset": 28800,
"timezone": "CST",
"exchangeTimezoneName": "Asia/Taipei",
"regularMarketPrice": 78.5,
"chartPreviousClose": 76.9,
"priceHint": 2,
"currentTradingPeriod": {
"pre": {
"timezone": "CST",
"start": 1599786000,
"end": 1599786000,
"gmtoffset": 28800
},
"regular": {
"timezone": "CST",
"start": 1599786000,
"end": 1599802200,
"gmtoffset": 28800
},
"post": {
"timezone": "CST",
"start": 1599802200,
"end": 1599802200,
"gmtoffset": 28800
}
},
"dataGranularity": "1d",
"range": "",
"validRanges": [
"1d",
"5d",
"1mo",
"3mo",
"6mo",
"1y",
"2y",
"5y",
"10y",
"ytd",
"max"
]
},
"timestamp": [
1598922000,
1599008400,
1599094800,
1599181200,
1599440400,
1599526800,
1599613200,
1599699600,
1599786000
],
"indicators": {
"quote": [
{
"volume": [
18151104,
24934189,
41374369,
35776710,
20795188,
22118917,
22505471,
29117204,
16102726
],
"high": [
77.30000305175781,
77.80000305175781,
79.30000305175781,
77.30000305175781,
77.5,
78.0999984741211,
77.9000015258789,
78.69999694824219,
78.5
],
"low": [
76.4000015258789,
77.0,
77.5999984741211,
76.5999984741211,
76.80000305175781,
77.30000305175781,
76.5999984741211,
78.0,
78.0
],
"close": [
77.0,
77.80000305175781,
77.69999694824219,
76.9000015258789,
77.30000305175781,
77.5,
77.9000015258789,
78.5,
78.5
],
"open": [
76.9000015258789,
77.19999694824219,
78.80000305175781,
77.0,
76.80000305175781,
77.69999694824219,
77.30000305175781,
78.0,
78.5
]
}
],
"adjclose": [
{
"adjclose": [
77.0,
77.80000305175781,
77.69999694824219,
76.9000015258789,
77.30000305175781,
77.5,
77.9000015258789,
78.5,
78.5
]
}
]
}
}
],
"error": null
}
}
其中timestamp
內的秒數轉成日期為:
close
為:去證交所抓資料可以看到這幾天收盤價是:
當無法執行時,應該是我們的執行環境設定不對,像是我們這時候就要選擇正確的虛擬環境。
要執行我們這個檔案,可以用兩個方式:
在terminal中執行
先執行pipenv shell
,他就會變成
(12-ithome) bash-3.2$
我們可以檢查環境,Mac OS預設是Python2,如果是我們設定的環境,應該會是Python3.8,所以可以輸入(12-ithome) bash-3.2$ python --version
,結果會是:
Python 3.8.5
這時可以放心地執行檔案:
(12-ithome) bash-3.2$ python Day5_getStockFromYahoo.py
結果也會是:
{"chart":{"result":[{"meta":{"currency":"TWD","symbol":"2317.TW","exchangeName":"TAI","instrumentType":"EQUITY","firstTradeDate":726195600,"regularMarketTime":1599802203,"gmtoffset":28800,"timezone":"CST","exchangeTimezoneName":"Asia/Taipei","regularMarketPrice":78.5,"chartPreviousClose":76.9,"priceHint":2,"currentTradingPeriod":{"pre":{"timezone":"CST","start":1599786000,"end":1599786000,"gmtoffset":28800},"regular":{"timezone":"CST","start":1599786000,"end":1599802200,"gmtoffset":28800},"post":{"timezone":"CST","start":1599802200,"end":1599802200,"gmtoffset":28800}},"dataGranularity":"1d","range":"","validRanges":["1d","5d","1mo","3mo","6mo","1y","2y","5y","10y","ytd","max"]},"timestamp":[1598922000,1599008400,1599094800,1599181200,1599440400,1599526800,1599613200,1599699600,1599786000],"indicators":{"quote":[{"open":[76.9000015258789,77.19999694824219,78.80000305175781,77.0,76.80000305175781,77.69999694824219,77.30000305175781,78.0,78.5],"close":[77.0,77.80000305175781,77.69999694824219,76.9000015258789,77.30000305175781,77.5,77.9000015258789,78.5,78.5],"volume":[18151104,24934189,41374369,35776710,20795188,22118917,22505471,29117204,16102726],"high":[77.30000305175781,77.80000305175781,79.30000305175781,77.30000305175781,77.5,78.0999984741211,77.9000015258789,78.69999694824219,78.5],"low":[76.4000015258789,77.0,77.5999984741211,76.5999984741211,76.80000305175781,77.30000305175781,76.5999984741211,78.0,78.0]}],"adjclose":[{"adjclose":[77.0,77.80000305175781,77.69999694824219,76.9000015258789,77.30000305175781,77.5,77.9000015258789,78.5,78.5]}]}}],"error":null}}
在Visual Stadio Code直接運作:
選擇好執行的環境(可在上圖一調整),然後選擇執行Python就可以囉!
不過每次都需要這樣按有點麻煩,而且不能使用中斷點,所以我們直接在設定檔做好,每次按F5
就可以囉!不知道的話,可以參考官方文件。
參考網站:
Yahoo Finance 已新增反爬蟲機制了哦!需加上 header
該面對的還是要面對=口=
沒錯,另外想請教其他篇文章的疑問